﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
using PsychveyApp1.classes;

namespace PsychveyApp1
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["Username"] != null && Request.Cookies["Password"] != null)
            {
                tbxUsername.Text = Request.Cookies["Username"].Value;
                tbxPassword.Attributes["value"] = Request.Cookies["Password"].Value;
            }
        }

        protected void btnUserSignin_Click(object sender, EventArgs e)
        {
            if (chkRememberMe.Checked)
            {
                Response.Cookies["Username"].Expires = DateTime.Now.AddDays(30);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
            }
            else
            {
                Response.Cookies["Username"].Expires = DateTime.Now.AddDays(-1);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);

            }
            Response.Cookies["Username"].Value = tbxUsername.Text.Trim();
            Response.Cookies["Password"].Value = tbxUsername.Text.Trim();

            UserAccount useraccount = new UserAccount();
            string usertext = tbxUsername.Text;
            Session["username"] = usertext; 
            string passtext = tbxPassword.Text;
            Session["password"] = passtext; 

            useraccount = DBManager.UserLogin(usertext,passtext);
                if(!useraccount.username.Equals(""))
                {
                    if (useraccount.accountTypeSelected == "Basic")
                    {
                        Session["currentUser"] = useraccount;
                        Response.Redirect("UserHome.aspx");
                    }
                    else if (useraccount.accountTypeSelected == "Premium")
                    {
                        Response.Redirect("UserHomePremium.aspx");
                    }
                }
                else
                {
                    lblStatusUser.Text = ("Username or password is incorrect!");
                }
            }
        }
    }
